<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/18
 * Time: 14:23
 */

namespace app\zucai\home;
use app\index\controller\Home;
use think\Db;
use app\zucai\model\Order;

/**
 * Class Cont
 * @package app\zucai\home
 * 投注信息查询
 */
class Cont extends Home
{
    //用户投注详情接口(仅未开奖数据)
    public function index(){ //传参为用户id
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
                //总投注情况
                $con = Db::name('admin_order')
                    ->where('isdraw','in','1,2,3')
					->where('oddtime','>',date('Y-m-d', strtotime('-7 days')))
                    ->where('uid', $post['id'])
					->order('oddtime DESC')
                    ->select();

                $dat= Db::name('playrule')->field('money')->where('id',1)->find();
                foreach ($con as $k=>$v){
					if($post['id']==$con[$k]['uid']){
						$con[$k]['person']=1;
					}else{
						$con[$k]['person']=0;
					}
					
				 $number = Order::where('pid', $con[$k]['uid'])
                ->where('order_id',$con[$k]['trano'])               
                ->where('type', 1)
                ->count();
					
					
					if($number>20 ){
                //热度
                $con[$k]['re'] =5;
	         	}elseif($number>15 && $number<=20){
                //热度
                $con[$k]['re'] =4;
            }elseif($number>10 && $number<=15){
                //热度
            $con[$k]['re'] =3;
           }elseif($number>5 && $number<=10){
                //热度
                $con[$k]['re'] =2;
            }elseif($number>0 && $number<=5){
                //热度
                $con[$k]['re'] =1;
            }elseif($number==0 ){
                //热度
                $con[$k]['re'] =0;
            }
					
					
                    $con[$k]['price']=$dat['money'];
                    $time=[];
                    $fla=explode(',',$con[$k]['choice']);
                    foreach ($fla as $key=>$value ){
                        $flag=explode('=>',$value);

                        $f=Db::name('play')->field('firsttime')->where('id',$flag[0])->find();
                        $time[$key]=$f['firsttime'];
                    }
                    array_multisort($time,SORT_REGULAR);

                    
					
					$tim=strtotime($time[0]);
					
					      
               if(time()>strtotime($time[0])){ //当前比赛都已结束时
					
                        $con[$k]['fal']=1;
                        
                    }else{
                        $con[$k]['fal']=0;
                    }
			
			 if(date('w',$tim)==0 || date('w',$tim)==1){  
			  
                            if(date('G', $tim) >= 1 && date('G', $tim) < 10) { //比赛在凌晨1点-10点时
                             
                                   $con[$k]['firsttime']=date("Y-m-d",$tim).' 0:30:00';                              
                            } else {
                               
                                   $con[$k]['firsttime']=date("Y-m-d H:i:s",strtotime("- 30 minute",strtotime($time[0])));
							}
                                
                        }else{
                            if(date('G',$tim) >= 0 && date('G',$tim) < 10) { //比赛在凌晨0点-10点时
                               
							if ((date('d', $tim) - date('d')) ==1) {  //今天买明天的票
                               $con[$k]['firsttime']=date("Y-m-d").' 23:30:00';
							 }else{
								 $con[$k]['firsttime']=date("Y-m-d",$tim-3600*24).' 23:30:00';
							 }
                            } else {
                                $con[$k]['firsttime']=date("Y-m-d H:i:s",strtotime("- 30 minute",strtotime($time[0])));
                            }
							
                        }
						
						if( time()>strtotime($con[$k]['firsttime'])){
								$con[$k]['stop']=1;		
								 }else{
									 $con[$k]['stop']=0;
								 }

                }
				
				

                if($con){
                    return returnOk($con);

                }else{
                    return returnError('该用户暂无投注方案！');
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }
	public function forder(){
		 if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
				$con = Db::name('order')
                    ->field('choice,choicetype,itemcount,beishu,lowpei')
                    ->where('id', $post['orderid'])
                    ->where('uid', $post['id']) 
                    ->find();
				if(!$con) return returnError('暂无符合条件的订单!');
				 $fla=explode(',',$con['choice']);
				 $kk=0;
				   foreach ($fla as $k=>$v ){
                        $flag=explode('=>',$v);
                        $f=Db::name('play')->field('firsttime')->where('id',$flag[0])->find();
                        $time[$k]=$f['firsttime'];

                        $date['choice'][$k] = explode('=>', $v);
                        $date['choice'][$k][1] = explode('-', $date['choice'][$k][1]);

                        $cont = Db::name('play')->where('id', $date['choice'][$k][0])->find();
						 $kai = Db::name('kaijiang')->where('play_id', $date['choice'][$k][0])->find();
                        
						
                        foreach ($date['choice'][$k][1] as $k3 => $v3) {
							
                        $con['team'][$kk]['ftitle']=$cont['ftitle'];
                        $con['team'][$kk]['team'] = $cont['team'].'--'.$cont['team2'] ;
						
                            $date['choice'][$k][1][$k3] = explode('@', $v3);

                            if ($date['choice'][$k][1][$k3][0] == 'win') {
								
                                $con['team'][$kk]['type'] =  '胜平负';
							    $con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['win_money'].')';
								$con['team'][$kk]['result'] =  $kai['win'];
								
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'ran') {
                               
								$con['team'][$kk]['type'] =  '让球胜平负';
							    $con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['rwin_money'].')';
								$con['team'][$kk]['result'] =  $kai['rwin'];
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'fen') {
                               
								$con['team'][$kk]['type'] =  '比分';
							    $con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['score_money'].')';
								$con['team'][$kk]['result'] =  $kai['score'];
								
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'all') {
                              
								$con['team'][$kk]['type'] =  '总进球数';
							    $con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['goal_money'].')';
								$con['team'][$kk]['result'] =  $kai['goal'];
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'half') {
                                $con['team'][$kk][$k3+1] = '半全场胜负平：' . $date['choice'][$k][1][$k3][1];
								
								$con['team'][$kk]['type'] =  '半全场胜负平';
							    $con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['double_money'].')';
								$con['team'][$kk]['result'] =  $kai['doub'];
								$kk++;
                            }
                        }
                    }

				 if($con){
                    return returnOk($con);
                }else{
                    return returnError('暂无该用户！');
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
	}

    //用户单个投注详情
    public function userorder(){//传参为订单id 用户id
        if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
				
                $con = Db::name('order')
                    ->field('uid,pid,order_id,isdraw,trano,choice,choicetype,itemcount,beishu,type,mode,okamount,oddtime,lowpei,content,pub')
                    ->where('id', $post['orderid'])
                    ->where('uid', $post['id'])
                    
                    ->find();
					  if($con['isdraw']==2 || $con['isdraw']==3){
							$con['kai']=1;
						}else{
							$con['kai']=0;
						}
				
						if($con['isdraw']==0){
							$con['isdraw']='进行中';
						}else{
							$con['isdraw']='委托成功';
						}
						
						
					
					$user = Db::name('user')
                    ->field('name,avatar')
                    ->where('id', $con['uid'])                                 
                    ->find();
					
					$con['user']=$user['name'];
					if(in_array($user['avatar'],array(1,2,3,4,5,6,7,8,9,10,11))){
                    $con['avatar']= get_file_path($user['avatar']);
                }else{
					$con['avatar']=$user['avatar'];
				}
					
                    $time=[];
                    $fla=explode(',',$con['choice']);
					if(!$con) return returnError('暂无符合条件的订单!');
						$kk=0;$arr=[];$ar=0;
						
                    foreach ($fla as $k=>$v ){
                        $flag=explode('=>',$v);
                        $f=Db::name('play')->field('firsttime')->where('id',$flag[0])->find();
                        $time[$k]=$f['firsttime'];

                        $date['choice'][$k] = explode('=>', $v);
                        $date['choice'][$k][1] = explode('-', $date['choice'][$k][1]);

                        $cont = Db::name('play')->where('id', $date['choice'][$k][0])->find();
						 $kai = Db::name('kaijiang')->where('play_id', $date['choice'][$k][0])->find();
						 $kp=0;
                        if(!$kai){
							$kai['win']='-';
							$kai['rwin']='-';
							$kai['score']='-';
							$kai['goal']='-';
							$kai['doub']='-';
							$kp=1;
						}
						
						
                        foreach ($date['choice'][$k][1] as $k3 => $v3) {
	
                            $date['choice'][$k][1][$k3] = explode('@', $v3);
							if($date['choice'][$k][1][$k3][1]!=''){
								
								if(time()>=strtotime($time[$k])){ //当前比赛已开打 时
								
					              $arr[$ar]['ftitle']=$cont['ftitle'];
                                  $arr[$ar]['team'] = $cont['team'].'--'.$cont['team2'] ;;
							 if ($date['choice'][$k][1][$k3][0] == 'win') {
								
                                $arr[$ar]['type'] =  '胜平负';
								if($kp==1){
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['win_money'].')';
								}
							    
								$arr[$ar]['result'] =  $kai['win'];
								
								$ar++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'ran') {
                               
								$arr[$ar]['type'] =  '让球胜平负';
							   
								if($kp==1){
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['rwin_money'].')';
								}
								$arr[$ar]['result'] =  $kai['rwin'];
								$ar++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'fen') {
                               
								$arr[$ar]['type'] =  '比分';
							    
								if($kp==1){
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['score_money'].')';
								}
								$arr[$ar]['result'] =  $kai['score'];
								
								$ar++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'all') {
                              
								$arr[$ar]['type'] =  '总进球数';
							    
								if($kp==1){
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['goal_money'].')';
								}
								$arr[$ar]['result'] =  $kai['goal'];
								$ar++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'half') {
                               
								
								$arr[$ar]['type'] =  '半全场胜负平';
							   
								if($kp==1){
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$arr[$ar]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['double_money'].')';
								}
								$arr[$ar]['result'] =  $kai['doub'];
								$ar++;
                            }
                                  }
								 $con['team'][$kk]['ftitle']=$cont['ftitle'];
                                 $con['team'][$kk]['team'] = $cont['team'].'--'.$cont['team2'] ;
								 
								 
                            if ($date['choice'][$k][1][$k3][0] == 'win') {
								
                                $con['team'][$kk]['type'] =  '胜平负';
								if($kp==1){
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['win_money'].')';
								}
							    
								$con['team'][$kk]['result'] =  $kai['win'];
								
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'ran') {
                               
								$con['team'][$kk]['type'] =  '让球胜平负';
							   
								if($kp==1){
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['rwin_money'].')';
								}
								$con['team'][$kk]['result'] =  $kai['rwin'];
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'fen') {
                               
								$con['team'][$kk]['type'] =  '比分';
							    
								if($kp==1){
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['score_money'].')';
								}
								$con['team'][$kk]['result'] =  $kai['score'];
								
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'all') {
                              
								$con['team'][$kk]['type'] =  '总进球数';
							    
								if($kp==1){
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['goal_money'].')';
								}
								$con['team'][$kk]['result'] =  $kai['goal'];
								$kk++;
                            } elseif ($date['choice'][$k][1][$k3][0] == 'half') {
                               
								
								$con['team'][$kk]['type'] =  '半全场胜负平';
							   
								if($kp==1){
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']';
								}else{
									$con['team'][$kk]['choice'] ='['.$date['choice'][$k][1][$k3][1].']('.$kai['double_money'].')';
								}
								$con['team'][$kk]['result'] =  $kai['doub'];
								$kk++;
                            }
							}
                        }
                    }
					
					
					
					
					

                    array_multisort($time,SORT_REGULAR);
					 if(time()>=strtotime($time[0])){ //当前比赛已开打 时
					
                        $con['fal']=1;
                        
                    }else{
                        $con['fal']=0;
                    }
					$tim=strtotime($time[0]);
			
			 if(date('w',$tim)==0 || date('w',$tim)==1){  
			  
                            if(date('G', $tim) >= 1 && date('G', $tim) < 10) { //比赛在凌晨1点-10点时
                             
                                   $con['tim']=date("Y-m-d",$tim).' 0:30:00';                              
                            } else {
                               
                                   $con['tim']=date("Y-m-d H:i:s",strtotime("- 30 minute",strtotime($time[0])));
							}
                                
                        }else{
                            if(date('G',$tim) >= 0 && date('G',$tim) < 10) { //比赛在凌晨0点-10点时
                                
								if ((date('d', $tim) - date('d')) ==1) {  //今天买明天的票
                               $con['tim']=date("Y-m-d").' 23:30:00';
							 }else{
								 $con['tim']=date("Y-m-d",$tim-3600*24).' 23:30:00';
							 }
                            } else {
                                $con['tim']=date("Y-m-d H:i:s",strtotime("- 30 minute",strtotime($time[0])));
                            }
                        }
					
				if($post['uid']==$con['uid']){
						$con['person']=1;
						if($con['type']==1){
							$con['team']=$arr;
						}
					  }else{
					 	$con['person']=0;
						
						$con['team']=$arr;
					  }
					 
                  if($con['type']==1){ //该订单为跟单 查询被跟单人的投注信息
                            $da = Db::name('user_gendan')
                                ->field('name,trano,uid,trano')
                                ->where('uid', $con['pid'])                              
                                ->where('trano', $con['order_id'])
                                ->find();
								
                            $con['puser']=$da['name'];
                            $con['ptrano']=$da['trano'];
							
							$dat = Db::name('user_gendan')
                                ->field('name,itemcount,mode')
								->where('isdraw','in','1,2,3')
                                ->where('pid', $da['uid'])
                                ->where('order_id', $da['trano'])
								->order('itemcount DESC')
                                ->limit(5)								
                                ->select();
								
                        }else{               //该订单为认购 查询跟其下单的用户数量及信息
						
								//总中奖佣金
             
			   $dat = Db::name('user_gendan')
                                ->field('mode')
                                ->where('pid', $con['uid'])
                                ->where('order_id', $con['trano'])
							->where('isdraw','in','1,2,3')
								->order('itemcount DESC')                            							
                                ->select();
                 
					  $amoney=0;
					  if(count($dat)>0){
					           foreach($dat as $key=>$value){									
									$amoney+=$value['mode']*0.07;
									$con['amoney']=round($amoney,2);									
								}
								
								
					  }else{
						  $con['amoney']=0;
					  }
					
						if($con['person']==1){
							$dat = Db::name('user_gendan')
                                ->field('name,itemcount,mode')
                                ->where('pid', $con['uid'])
								->where('isdraw','in','1,2,3')
                                ->where('order_id', $con['trano'])
								->order('itemcount DESC')                            							
                                ->select();
						
					      }else{
						
							$dat = Db::name('user_gendan')
                                ->field('name,itemcount,mode')
								->where('isdraw','in','1,2,3')
                                ->where('pid', $con['uid'])
                                ->where('order_id', $con['trano'])
								->order('itemcount DESC')
                                ->limit(5)								
                                ->select();
						}
    
                        }
						 foreach($dat as $key=>$value){
								
									if($value['mode']==0){
										$dat[$key]['mode']='-';
										$dat[$key]['ymoney']='-';
										
									}else{
									$dat[$key]['ymoney']=round($value['mode']*0.07,2);
									}
								}
								
						$con['pdata']=$dat;
                   
                if($con){
                    return returnOk($con);
                }else{
                    return returnError('暂无该用户！');
                }
            }else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }
    //
    //跟单页面数据接口
    public function gendan(){
		 if($this->request->isPost()){
            if($this->request->post()){
                $post=$this->request->post();
				$con = Db::name('admin_user')->field('id')
				->where('sign',$post['sign'])			
				->find();
        $data_list = Db::name('user_gendan')
            ->field('id,uid,name,avatar,trano,choice,choicetype,itemcount,beishu,oddtime,lowpei,content,pub')
            ->where('type', 0)
            ->where('isdraw', 1)
            ->where('gendan', 1)
            ->where('status', 1)
			->where('aid',$con['id'])
            ->select();
			
        foreach ($data_list as $k=>$v){
            $time=[];
            $fla=explode(',',$data_list[$k]['choice']);
		
            foreach ($fla as $key=>$value ){
                $flag=explode('=>',$value);

                $f=Db::name('play')->field('firsttime')->where('id',$flag[0])->find();
				
                $time[$key]=$f['firsttime'];
            }
            array_multisort($time,SORT_REGULAR);

            
			$tim=strtotime($time[0]);
		
			           if(date('w',$tim)==0 || date('w',$tim)==1){  
			  
                            if(date('G', $tim) >= 1 && date('G', $tim) < 10) { //比赛在凌晨1点-10点时
                             
                                             $data_list[$k]['firsttime']=date("Y-m-d",$tim).' 0:30:00';                                          											 
                            } else {
                               
                                   $data_list[$k]['firsttime']=date("Y-m-d H:i:s",strtotime("- 30 minute",strtotime($time[0])));
							}
                                
                        }else{
							
                            if(date('G',$tim) >= 0 && date('G',$tim) < 10) { //比赛在凌晨0点-10点时
								
							 if ((date('d', $tim) - date('d')) ==1) {  //今天买明天的票
                                $data_list[$k]['firsttime']=date("Y-m-d").' 23:30:00';
							 }else{
								  $data_list[$k]['firsttime']=date("Y-m-d",$tim-3600*24).' 23:30:00';
							 }
                            } else {
                                $data_list[$k]['firsttime']=date("Y-m-d H:i:s",strtotime("- 30 minute",strtotime($time[0])));
                            }
                        }
						
        }
		


		$f=count($data_list);
		for($i=0;$i<$f;$i++){
			if( time()>strtotime($data_list[$i]['firsttime'])){
							
										 unset($data_list[$i]);
										
																			
									 }
		}
		 $data_list=array_merge($data_list);
	
        //计算跟单人数及跟单总金额
        for ($i = 0; $i < count($data_list); $i++) {
            if(in_array($data_list[$i]['avatar'],array(1,2,3,4,5,6,7,8,9,10,11))){
                $data_list[$i]['avatar']= get_file_path($data_list[$i]['avatar']);
            }
            $cont = Order::field('username,itemcount,beishu')
                ->where('pid', $data_list[$i]['uid'])
                ->where('order_id',$data_list[$i]['trano'])
                ->where('isdraw', 1)
                ->where('type', 1)
                ->select();
            $money = 0;
			
			if($cont){
            foreach ($cont as $k => $v) {
                $money += $v['itemcount'];
            }
            $data_list[$i]['number'] = count($cont);
            $data_list[$i]['money'] = $money;
            $data_list[$i]['people']=$cont;
			
			}else{
				$data_list[$i]['number'] =0;
            $data_list[$i]['money'] = 0;
            $data_list[$i]['people']=0;
			}
        }
		
        //计算发单人的中奖金额及胜率等
        for ($i = 0; $i < count($data_list); $i++) {
            $cont = Order::where('uid', $data_list[$i]['uid'])
                ->where('isdraw', 2)
                ->select();
            $tcont = Order::where('uid', $data_list[$i]['uid'])
                ->where('isdraw', 'in','2,3')
                ->count();
            $allmoney = 0;
            $alltou = 0;
            foreach ($cont as $k => $v) {
                $allmoney += $v['mode'];
                $alltou += $v['itemcount'];
            }
            //总中奖金额
            $data_list[$i]['allmoney'] = $allmoney;
            //总投注金额
            $data_list[$i]['alltou'] =$alltou;
            //总投注次数
            $data_list[$i]['allcount'] =count($cont);
			
            //中奖率
			if($tcont==0){
				$data_list[$i]['pei'] = '0%';
			}else{
				$data_list[$i]['pei'] = round((count($cont)/$tcont * 100),2).'%';
			}
            
        }
//根据跟单总金额排序
        for ($i = 0; $i < count($data_list); $i++) {
            for ($j = $i+1; $j <count($data_list) ; $j++) {
                if ($data_list[$i]['itemcount'] < $data_list[$j]['itemcount']) {
                    $iTemp = $data_list[$j];
                    $data_list[$j] = $data_list[$i];
                    $data_list[$i] = $iTemp;
                }
            }
            if($data_list[$i]['number']>20 ){
                //热度
                $data_list[$i]['re'] =5;
		}elseif($data_list[$i]['number']>15 && $data_list[$i]['number']<=20){
                //热度
                $data_list[$i]['re'] =4;
            }elseif($data_list[$i]['number']>10 && $data_list[$i]['number']<=15){
                //热度
            $data_list[$i]['re'] =3;
           }elseif($data_list[$i]['number']>5 && $data_list[$i]['number']<=10){
                //热度
                $data_list[$i]['re'] =2;
            }elseif($data_list[$i]['number']>0 && $data_list[$i]['number']<=5){
                //热度
                $data_list[$i]['re'] =1;
            }elseif($data_list[$i]['number']==0 ){
                //热度
                $data_list[$i]['re'] =0;
            }
        }
        if($data_list){
            return returnOk($data_list);
        }else{
            return returnOk('暂无数据');
        }
		}else{
                return returnError('post传值错误！');
            }
        }else{
            return returnError('请使用POST方式请求');
        }
    }

}